home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GS.C < prev    next >
C/C++ Source or Header  |  1992-03-20  |  12KB  |  393 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gs.c */
  21. /* Driver program for Ghostscript */
  22. #include <stdio.h>
  23. #include "memory_.h"
  24. #include "string_.h"
  25. #include "ghost.h"
  26. #include "alloc.h"
  27. #include "estack.h"
  28. #include "ostack.h"
  29. #include "store.h"
  30. #include "stream.h"
  31.  
  32. /* Library routines not declared in a standard header */
  33. extern char *getenv(P1(const char *));
  34.  
  35. /* Exported data */
  36. uint memory_chunk_size = 20000;
  37. /* File name search paths */
  38. const char **gs_lib_paths;
  39. private int gs_lib_count;
  40. private const char *gs_lib_env_path;
  41.  
  42. /* Configuration information imported from gconfig.c. */
  43. extern const char *gs_lib_default_path;
  44. extern const char *gs_init_file;
  45.  
  46. /* Device procedures imported from gsdevice.c. */
  47. typedef struct gx_device_s gx_device;
  48. extern gx_device *gs_getdevice(P1(int));
  49. extern char *gs_devicename(P1(gx_device *));
  50.  
  51. /* Help string */
  52. private char *gs_help1 = "\
  53. Usage: gs [switches] [file1.ps file2.ps ...]\n\
  54.   or : gs [switches] [file1.ps ...] -- filen.ps arg1 arg2 ...\n\
  55. The latter passes arg1 ... to the program in filen.ps.\n\
  56. Available devices:\n   ";
  57. /* We have to break help2 up into two parts, because the Watcom compiler */
  58. /* has a limit of 510 characters for a single token. */
  59. private char *gs_help2a = "\n\
  60. Switches: (you can use # in place of =)\n\
  61.     -d<name>[=<token>]   define name as token, or null if no token given\n\
  62.     -g<width>x<height>   set width and height (`geometry') for device\n\
  63.     -I<prefix>           add prefix to search path\n\
  64.     -q                   `quiet' mode, suppress most messages\n\
  65.     -r<res>              set resolution for initial device\n";
  66. private char *gs_help2b = "\
  67.     -r<xres>x<yres>      set device X and Y resolution separately\n\
  68.     -s<name>=<string>    define name as string\n\
  69.     -sDEVICE=<devname>   select initial device\n\
  70.     -sOUTPUTFILE=<file>  select output file, embed %d for page #,\n\
  71.                            |command to pipe\n\
  72. `-' alone as a file name means read from stdin non-interactively.\n\
  73. For more information, please read the use.doc file.\n";
  74.  
  75. /* Forward references */
  76. private void runarg(P4(char **, char *, char *, int));
  77. private void run_string(P1(char *));
  78. private void init1(), init2();
  79. private void set_lib_paths();
  80. private void run_file(P2(const char *file_name, int user_errors));
  81. private void debug_dump_stack(P1(int code));
  82.  
  83. /* Parameters set by swproc */
  84. private int user_errors;
  85. private int quiet;
  86. private int batch;
  87.  
  88. /* Static versions of argc and argv (for -- only) */
  89. private int static_argc;
  90. private char **static_argv;
  91.  
  92. main(int argc, char *argv[])
  93. {    int num_files;
  94.     int swproc(P2(char **, char *));
  95.     void argproc(P2(char **, int));
  96.     static_argc = argc;
  97.     static_argv = argv;
  98.     /* Do platform-dependent initialization. */
  99.     /* We have to do this as the very first thing, */
  100.     /* because it detects attempts to run 80N86 executables (N>0) */
  101.     /* on incompatible processors. */
  102.     gp_init();
  103.     /* Initialize the file search paths */
  104.     gs_lib_env_path = 0;
  105.        {    char *lib = getenv("GS_LIB");
  106.         if ( lib != 0 ) 
  107.            {    int len = strlen(lib);
  108.             gs_lib_env_path = gs_malloc(len + 1, 1, "GS_LIB");
  109.             strcpy((char *)gs_lib_env_path, lib);
  110.            }
  111.        }
  112.     gs_lib_paths =
  113.         (const char **)gs_malloc(argc + 2, sizeof(char *), "-I array");
  114.     gs_lib_count = 0;
  115.     set_lib_paths();
  116.     /* Execute files named in the command line, */
  117.     /* processing options along the way. */
  118.     /* Wait until the first file name (or the end */
  119.     /* of the line) to finish initialization. */
  120.     batch = 0;
  121.     quiet = 0;
  122.     user_errors = 1;
  123.     num_files = gs_main(argc, argv, "GS.MAP", swproc, argproc);
  124.     if ( num_files == 0 )
  125.        {    init2();
  126.        }
  127.     if ( !batch ) run_string("start");
  128.     gs_exit(0);
  129. }
  130.  
  131. /* Process switches */
  132. int
  133. swproc(char **swp, char *arg)
  134. {    char sw = (*swp)[1];
  135.     switch ( sw )
  136.        {
  137.     default:
  138.         return -1;
  139.     case 0:                /* read stdin as a file */
  140.         batch = 1;
  141.         /* Set NOPAUSE so showpage won't try to read from stdin. */
  142.            {    char *d = "-dNOPAUSE";
  143.             swproc(&d, d);
  144.            }
  145.         init2();        /* Finish initialization */
  146.         run_string("(%stdin) (r) file cvx execute");
  147.         break;
  148.     case '-':            /* run with command line args */
  149.     case '+':            /* ditto */
  150.        {    int nstrs = static_argv + static_argc - (swp + 2);
  151.         if ( nstrs < 0 )    /* no file to run! */
  152.            {    printf("Usage: gs ... -- file.ps arg1 ... argn\n");
  153.             gs_exit(1);
  154.            }
  155.         runarg(swp + 1, "{userdict /ARGUMENTS [", "] put (", nstrs);
  156.        }
  157.         gs_exit(0);
  158.     case 'h':            /* print help */
  159.     case '?':
  160.         fputs(gs_help1, stdout);
  161.            {    int i;
  162.             gx_device *pdev;
  163.             for ( i = 0; (pdev = gs_getdevice(i)) != 0; i++ )
  164.                 printf(" %s", gs_devicename(pdev));
  165.            }
  166.         fputs(gs_help2a, stdout);
  167.         fputs(gs_help2b, stdout);
  168.         gs_exit(0);
  169.     case 'I':            /* specify search path */
  170.         gs_lib_paths[gs_lib_count] = arg;
  171.         gs_lib_count++;
  172.         set_lib_paths();
  173.         break;
  174.     case 'q':            /* quiet startup */
  175.        {    ref vnull;
  176.         quiet = 1;
  177.         init1();
  178.         make_null(&vnull);
  179.         initial_enter_name("QUIET", &vnull);
  180.        }    break;
  181.     case 'D':            /* define name */
  182.     case 'd':
  183.     case 'S':            /* define name as string */
  184.     case 's':
  185.        {    char *eqp = strchr(arg, '=');
  186.         int isd = (sw == 'D' || sw == 'd');
  187.         ref value;
  188.         if ( eqp == NULL ) eqp = strchr(arg, '#');
  189.         /* Initialize the object memory, scanner, and */
  190.         /* name table now if needed. */
  191.         init1();
  192.         if ( eqp == arg )
  193.            {    printf("Usage: -dname, -dname=token, -sname=string\n");
  194.             gs_exit(1);
  195.            }
  196.         if ( eqp == NULL )
  197.            {    if ( isd ) make_null(&value);
  198.             else make_tasv(&value, t_string, a_read+a_execute,
  199.                        0, bytes, (byte *)"");
  200.            }
  201.         else
  202.            {    int code;
  203.             *eqp++ = 0;    /* delimit name */
  204.             if ( isd )
  205.                {    stream astream;
  206.                 sread_string(&astream,
  207.                          (byte *)eqp, strlen(eqp));
  208.                 code = scan_token(&astream, 0, &value);
  209.                 if ( code )
  210.                    {    printf("-dname= must be followed by a valid token\n");
  211.                     gs_exit(1);
  212.                    }
  213.                }
  214.             else
  215.                {    int len = strlen(eqp);
  216.                 char *str = gs_malloc((uint)len, 1, "-s");
  217.                 if ( str == 0 )
  218.                    {    lprintf("Out of memory!\n");
  219.                     gs_exit(1);
  220.                    }
  221.                 memcpy(str, eqp, len);
  222.                 make_tasv(&value, t_string, a_read+a_execute,
  223.                       len, bytes, (byte *)str);
  224.                }
  225.            }
  226.         /* Enter the name in systemdict */
  227.         initial_enter_name(arg, &value);
  228.         break;
  229.        }
  230.     case 'g':            /* define device geometry */
  231.        {    long width, height;
  232.         ref value;
  233.         init1();
  234.         if ( sscanf(arg, "%ldx%ld", &width, &height) != 2 )
  235.            {    printf("-g must be followed by <width>x<height>\n");
  236.             gs_exit(1);
  237.            }
  238.         make_int(&value, width);
  239.         initial_enter_name("DEVICEWIDTH", &value);
  240.         make_int(&value, height);
  241.         initial_enter_name("DEVICEHEIGHT", &value);
  242.         break;
  243.        }
  244.     case 'M':            /* set memory allocation increment */
  245.        {    unsigned msize = 0;
  246.         sscanf(arg, "%d", &msize);
  247.         if ( msize <= 0 || msize >= 64 )
  248.            {    printf("-M must be between 1 and 64\n");
  249.             gs_exit(1);
  250.            }
  251.         memory_chunk_size = msize << 10;
  252.        }
  253.         break;
  254.     case 'r':            /* define device resolution */
  255.        {    long xres, yres;
  256.         ref value;
  257.         init1();
  258.         switch ( sscanf(arg, "%ldx%ld", &xres, &yres) )
  259.            {
  260.         default:
  261.             printf("-r must be followed by <res> or <xres>x<yres>\n");
  262.             gs_exit(1);
  263.         case 1:            /* -r<res> */
  264.             yres = xres;
  265.         case 2:            /* -r<xres>x<yres> */
  266.             make_int(&value, xres);
  267.             initial_enter_name("DEVICEXRESOLUTION", &value);
  268.             make_int(&value, yres);
  269.             initial_enter_name("DEVICEYRESOLUTION", &value);
  270.            }
  271.         break;
  272.        }
  273.        }
  274.     return 0;
  275. }
  276.  
  277. /* Process file names */
  278. void
  279. argproc(char **argp, int index)
  280. {    runarg(argp, "{", "(", 0);
  281. }
  282. private void
  283. runarg(char **argp, char *pre, char *post, int nstrs)
  284. {    char *arg = *argp;
  285.     static char *pex = ")run}execute";
  286.     int len = strlen(pre) + strlen(arg) + strlen(post) + strlen(pex) + 1;
  287.     char *line;
  288.     int i;
  289.     for ( i = 1; i <= nstrs; i++ )
  290.         len += strlen(argp[i]) + 2;
  291.     init2();    /* Finish initialization */
  292.     line = gs_malloc(len, 1, "argproc");
  293.     if ( line == 0 )
  294.        {    lprintf("Out of memory!\n");
  295.         gs_exit(1);
  296.        }
  297.     strcpy(line, pre);
  298.     for ( i = 1; i <= nstrs; i++ )
  299.        {    strcat(line, "(");
  300.         strcat(line, argp[i]);
  301.         strcat(line, ")");
  302.        }
  303.     strcat(line, post);
  304.     strcat(line, arg);
  305.     strcat(line, pex);
  306.     run_string(line);
  307. }
  308. private void
  309. run_string(char *str)
  310. {    int code;
  311.     ref stref;
  312.     make_tasv(&stref, t_string, a_executable + a_read + a_execute,
  313.           strlen(str), bytes, (byte *)str);
  314.     code = gs_interpret(&stref, user_errors);
  315.     zflush((ref *)0);    /* flush stdout */
  316.     zflushpage((ref *)0); /* force display update */
  317.     if ( code ) debug_dump_stack(code), gs_exit(2);
  318. }
  319.  
  320. private int init1_done = 0, init2_done = 0;
  321. private void
  322. init1()
  323. {    if ( !init1_done )
  324.        {    alloc_init(gs_malloc, gs_free, memory_chunk_size);
  325.         name_init();
  326.         obj_init();        /* requires name_init */
  327.         scan_init();        /* ditto */
  328.         init1_done = 1;
  329.        }
  330. }
  331. private void
  332. init2()
  333. {    init1();
  334.     if ( !init2_done )
  335.        {    gs_init();
  336.         zop_init();
  337.         interp_init(1);        /* requires obj_init */
  338.         op_init();        /* requires obj_init, scan_init */
  339.         /* Execute the standard initialization file. */
  340.         run_file(gs_init_file, user_errors);
  341.         init2_done = 1;
  342.        }
  343.    }
  344.  
  345. /* Complete the list of library search paths. */
  346. private void
  347. set_lib_paths()
  348. {    const char **ppath = &gs_lib_paths[gs_lib_count];
  349.     if ( gs_lib_env_path != 0 ) *ppath++ = gs_lib_env_path;
  350.     if ( gs_lib_default_path != 0 ) *ppath++ = gs_lib_default_path;
  351.     *ppath = 0;
  352. }
  353.  
  354. /* Open and execute a file */
  355. private int
  356. run_open(const char *file_name, ref *pfile)
  357. {
  358. #define maxfn 200
  359.     byte fn[maxfn];
  360.     uint len;
  361.     return lib_file_open(file_name, strlen(file_name), fn, maxfn,
  362.                  &len, pfile);
  363. }
  364. private void
  365. run_file(const char *file_name, int user_errors)
  366. {    ref initial_file;
  367.     int code;
  368.     if ( run_open(file_name, &initial_file) < 0 )
  369.        {    eprintf1("Can't find initialization file %s\n", file_name);
  370.         gs_exit(1);
  371.        }
  372.     r_set_attrs(&initial_file, a_execute + a_executable);
  373.     code = gs_interpret(&initial_file, user_errors);
  374.     if ( code < 0 )
  375.         debug_dump_stack(code), gs_exit(1);
  376. }
  377.  
  378. /* Debugging code */
  379. extern void debug_print_ref(P1(ref *));
  380. extern void debug_dump_refs(P3(ref *, uint, char *));
  381. extern ref error_object;
  382.  
  383. /* Dump the stacks after interpretation */
  384. private void
  385. debug_dump_stack(int code)
  386. {    zflush(osp);    /* force out buffered output */
  387.     dprintf1("\nUnexpected interpreter error %d!\nError object: ", code);
  388.     debug_print_ref(&error_object);
  389.     dputc('\n');
  390.     debug_dump_refs(osbot, osp + 1 - osbot, "Operand stack");
  391.     debug_dump_refs(esbot, esp + 1 - esbot, "Execution stack");
  392. }
  393.